package com.yys.gulimall.member.controller;

import java.lang.reflect.Member;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.yys.common.exception.BizCodeEnume;
import com.yys.common.to.user.RegistUserTo;
import com.yys.gulimall.member.exception.PhoneExistException;
import com.yys.gulimall.member.exception.UsernameExistException;
import com.yys.gulimall.member.feign.CouponMemberService;
import com.yys.gulimall.member.vo.MemberLoginVo;
import com.yys.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yys.gulimall.member.entity.MemberEntity;
import com.yys.gulimall.member.service.MemberService;
import com.yys.common.utils.PageUtils;
import com.yys.common.utils.R;

import static com.yys.common.exception.BizCodeEnume.LOGINACCOUNT_PASSWORD_INVALID_EXCEPTION;


/**
 * 会员
 *
 * @author zqcc
 * @email zhang32194878@163.com
 * @date 2021-02-21 02:36:56
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponMemberService couponMemberService;

    //测试member远程调用 coupon
    @GetMapping("/test")
    public R CouponMember(){
        R r = couponMemberService.couponMemberTest();
        Object coupon = r.get("coupon");
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("阿离你起舞吖");
        return R.ok().put("member",memberEntity).put("coupon",coupon);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 注册
     * */
    @PostMapping("/regist")
    public R regist(@RequestBody RegistUserTo registUserTo) {
        try {
            memberService.regist(registUserTo);
        } catch (UsernameExistException e) {
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(),BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 登录
     * */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo) {
        MemberEntity memberEntity = memberService.login(memberLoginVo);
        if (ObjectUtils.isNull(memberEntity)) {
            return R.error(LOGINACCOUNT_PASSWORD_INVALID_EXCEPTION.getCode(),
                    LOGINACCOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
        return R.ok().setData(memberEntity);
    }

    /**
     * 社交登录
     * */
    @PostMapping("auth/login")
    public R socialLogin(@RequestBody SocialUser socialUser) {
        MemberEntity memberEntity = memberService.login(socialUser);
        if (ObjectUtils.isNull(memberEntity)) {
            return R.error(LOGINACCOUNT_PASSWORD_INVALID_EXCEPTION.getCode(),
                    LOGINACCOUNT_PASSWORD_INVALID_EXCEPTION.getMsg());
        }
        return R.ok().setData(memberEntity);
    }


}
